#include <Math.h>
#include "Texture.h"
#include "../HObject.h"
#include "../Domain/HelixImage.h"

namespace helix{

	TextureParameters::TextureParameters(unsigned char textureType)
	{
		this->textureType=textureType;
		minFilter=POINT_SAMPLE_FILTERING;
		maxFilter=POINT_SAMPLE_FILTERING;
		mipMapType=NO_MIP_MAPS;
		uRepeat=REPEAT;
		vRepeat=REPEAT;
		wRepeat=REPEAT;
	}

	TextureParameters::~TextureParameters()
	{
		
	}

	void TextureParameters::setTextureType(unsigned char textureType)
	{
		this->textureType=textureType;
	}
	void TextureParameters::setMinFilter(unsigned char minFilter)
	{
		this->minFilter=minFilter;
	}
	void TextureParameters::setMaxFilter(unsigned char maxFilter)
	{
		this->maxFilter=maxFilter;
	}
	void TextureParameters::setMipMapType(unsigned char mipMapType)
	{
		this->mipMapType=mipMapType;
	}
	void TextureParameters::setURepeat(unsigned char uRepeat)
	{
		this->uRepeat=uRepeat;
	}
	void TextureParameters::setVRepeat(unsigned char vRepeat)
	{
		this->vRepeat=vRepeat;
	}
	void TextureParameters::setWRepeat(unsigned char wRepeat)
	{
		this->wRepeat=wRepeat;
	}

	unsigned char TextureParameters::getTextureType()
	{
		return textureType;
	}
	unsigned char TextureParameters::getMinFilter()
	{
		return minFilter;
	}
	unsigned char TextureParameters::getMaxFilter()
	{
		return maxFilter;
	}
	unsigned char TextureParameters::getMipMapType()
	{
		return mipMapType;
	}
	unsigned char TextureParameters::getURepeat()
	{
		return uRepeat;
	}
	unsigned char TextureParameters::getVRepeat()
	{
		return vRepeat;
	}
	unsigned char TextureParameters::getWRepeat()
	{
		return wRepeat;
	}


	Texture::Texture(Image *image,TextureParameters *tParams)
	{
		if(image == NULL)
			memoryFootPrint=0;
		else if(!image->isLoaded())
			memoryFootPrint=0;
		else
			memoryFootPrint=image->getWidth()*image->getHeight()*image->getBitsPerPixel()/8;

		inGraphicsMemory = false;
	}

	Texture::~Texture()
	{
	}

	bool Texture::isPowerOf2(unsigned int length)
	{
		unsigned int test = 1;

		while(test != 0x00000000)
		{
			if(length == test)
				return true;
			test= test<<1;
		}

		return false;
	}
	unsigned int Texture::findNextPowerOf2(unsigned int length)
	{
		unsigned int power=1;	//start the return value at 2^0
		while(length != 0)
		{
			length = length >> 1;	//keep shifting the number to the right until there is nothing left
			power = power << 1;		//keep track of the amount of times we've shifted
		}

		return power;
	}

	unsigned int Texture::getMemoryFootprint()
	{	
		return memoryFootPrint;
	}

	bool Texture::isInGraphicsMemory()
	{
		return inGraphicsMemory;
	}
	
}